package com.ruoyi.common.map;

//演示get、post请求如何算sn，算得sn如何使用

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("deprecation")
public class MapUtil {
    private static final Logger log = LoggerFactory.getLogger(MapUtil.class);
    public static void main(String[] args) throws Exception {
        System.out.println(MapUtil.getLngLat("福建省泉州市丰泽区东海大街950号"));
    }

    @SuppressWarnings("all")
    public static String getLngLat(String address){
        try {
            /**
             * 以http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=yourak为例
             * ak设置了sn校验不能直接使用必须在url最后附上sn值，get请求计算sn跟url中参数对出现顺序有关，需按序填充paramsMap，
             * post请求是按字母序填充，具体参照testPost()
             */
            Map paramsMap = new LinkedHashMap<String, String>();
            paramsMap.put("address",address);
            paramsMap.put("output", "json");
            paramsMap.put("ak", "D9GplxDH9DmpYSaHCat7zP47RNTswvur");

            // 调用下面的toQueryString方法，对paramsMap内所有value作utf8编码
            String paramsStr = toQueryString(paramsMap);

            // 对paramsStr前面拼接上/geocoder/v2/?，后面直接拼接yoursk
            String wholeStr = new String("/geocoding/v3/?" + paramsStr + "P9Eer6HrpBGWKotyDAYRGwip3tkdu7gU");

            // 对上面wholeStr再作utf8编码
            String tempStr = URLEncoder.encode(wholeStr, "UTF-8");

            // 调用下面的MD5方法得到sn签名值d
            String sn = MD5(tempStr);

            // 算得sn后发送get请求
            HttpClient client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet( "http://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak=D9GplxDH9DmpYSaHCat7zP47RNTswvur&sn=" + sn);
            HttpResponse response = client.execute(httpget);
            InputStream is = response.getEntity().getContent();
            String result = inStream2String(is);
            JSONObject obj = JSONObject.parseObject(result);
            if (obj.get("status").toString().equals("0")) {
                double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
                double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
                return lng+","+lat;
            }else{
                log.error("查询不到地址");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return "";
    }

    // 对Map内所有value作utf8编码，拼接返回结果
    public static String toQueryString(Map<?, ?> data)
            throws UnsupportedEncodingException, URIException {
        StringBuffer queryString = new StringBuffer();
        for (Entry<?, ?> pair : data.entrySet()) {
            queryString.append(pair.getKey() + "=");
            // queryString.append(URLEncoder.encode((String) pair.getValue(),
            // "UTF-8") + "&");
            queryString.append(URIUtil.encodeQuery((String) pair.getValue(),
                    "UTF-8") + "&");
        }
        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }
        return queryString.toString();
    }

    // MD5计算方法，调用了MessageDigest库函数，并把byte数组结果转换成16进制
    public static String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

    // 将输入流转换成字符串
    private static String inStream2String(InputStream is) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = is.read(buf)) != -1) {
            baos.write(buf, 0, len);
        }
        return new String(baos.toByteArray(), "UTF-8");
    }
}